package onjava.arrays;

// arrays/CollectionComparison.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static onjava.ArrayShow.*;
import static onjava.Consts.*;
import onjava.Suppliers;

class BerylliumSphere {
  private static long counter;
  private final long id = counter++;

  @Override
  public String toString() {
    return "Sphere " + id;
  }
}

/**
 * 
 * 数值与泛型Collection的比较
 *
 * @author Lucky Yang
 */
public class CollectionComparison {
  /**
   * 程序入口
   * 
   * @param args
   */
  public static void main(String[] args) {
    // 创建数组1
    BerylliumSphere[] spheres = new BerylliumSphere[10];
    for (int i = 0; i < FIVE; i++) {
      spheres[i] = new BerylliumSphere();
    }
    show(spheres);
    System.out.println(spheres[4]);

    // 创建数组2
    List<BerylliumSphere> sphereList = Suppliers.create(
        ArrayList::new, BerylliumSphere::new, 5);
    System.out.println(sphereList);
    System.out.println(sphereList.get(4));

    // 创建数组3
    int[] integers = { 0, 1, 2, 3, 4, 5 };
    show(integers);
    System.out.println(integers[4]);

     // 创建数组4：自动装箱机制
    List<Integer> intList = new ArrayList<>(
        Arrays.asList(0, 1, 2, 3, 4, 5));
    intList.add(97);
    System.out.println(intList);
    System.out.println(intList.get(4));
  }
}
/*
 * Output:
 * [Sphere 0, Sphere 1, Sphere 2, Sphere 3, Sphere 4,
 * null, null, null, null, null]
 * Sphere 4
 * [Sphere 5, Sphere 6, Sphere 7, Sphere 8, Sphere 9]
 * Sphere 9
 * [0, 1, 2, 3, 4, 5]
 * 4
 * [0, 1, 2, 3, 4, 5, 97]
 * 4
 */
